<template>
  <div :id="id" :class="className" :style="{height:'258px',width:'100%'}" />
</template>

<script>
import * as echarts from "echarts";

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    list: {
      handler() {
        this.initChart()
      },
      deep: true
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        title: {
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            textStyle: {
              color: '#fff'
            }
          }
        },
        grid: {
          left: '5%',
          right: '5%',
          borderWidth: 0,
          top: 40,
          bottom: 20,
          textStyle: {
            color: '#fff'
          }
        },
        legend: {
          top: '0',
          textStyle: {
            color: '#fff'
          },
          itemHeight: 10,
          data: [
            {icon: 'circle', name: '电'},
            {icon: 'circle', name: '水'}
         ]
        },
        calculable: true,
        xAxis: [{
          type: 'category',
          axisLabel: {
            show: true,
            textStyle: {
                color: 'rgba(255, 255, 255, 0.8)', // 文字颜色
                fontSize: 12 // 文字的字体大小
            }
         },
         axisLine: {
            lineStyle: {
                color: 'rgba(255, 255, 255, 0.8)' // 设置轴线颜色为红色
            }
         },
         axisTick:{
          show: false,
         },
          data: this.list.map(i => i['date-format'])
        }],
        yAxis: [{
          axisLabel: {
            show: true,
            textStyle: {
                color: 'rgba(255, 255, 255, 0.8)', // 文字颜色
                fontSize: 12 // 文字的字体大小
            }
         },
          splitLine:{
            lineStyle:{
              type:'dashed',//y轴分割线类型 dashed为虚线
              color:'#3F4E68'
            }
          },
          type: 'value'
        }],
        series: [{
          name: '电',
          type: 'bar',
          // stack: 'sum',
          barMaxWidth: 15,
          barGap: '30',
          itemStyle: {
            normal: {
              color: '#128DEB',
              label: {
                show: false,
                textStyle: {
                  color: '#fff'
                },
                position: 'insideTop',
                formatter(p) {
                  return p.value > 0 ? p.value : ''
                }
              }
            }
          },
          data: this.list.map(i => i.use_ele)
        },

        {
          name: '水',
          type: 'line',
          // stack: 'sum',
          yAxisIndex: 1,
          itemStyle: {
            normal: {
              color: '#FCA431',
              barBorderRadius: 0,
              label: {
                show: false,
                position: 'top',
                formatter(p) {
                  return p.value > 0 ? p.value : ''
                }
              }
            }
          },
          data: this.list.map(i => i.use_water)
        }
        ],
      })
    }
  }
}
</script>
